package com.ourmovieguy;

import java.io.IOException;
import java.util.List;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class SearchMoviesServlet extends HttpServlet {

	private static final long serialVersionUID = -1912222855268518232L;
	
	@Override
	public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException{

		List<NYTMovie> movies = NYTMovieManager.getMoviesFromProvider(req.getParameter("Name"));
		for (NYTMovie movie : movies)
		{
			NYTMovie o = PM.getObjectById(NYTMovie.class, movie.getName());
			if (o == null)
				PM.makePersistent(movie);
		}
		
		if (movies != null)
		{
			String json = NYTMovieManager.getSmallJSON(movies);
			resp.addHeader("Content-Type", "application/x-javascript; charset=utf-8");
			resp.getWriter().write("resultCB(" + json + ");"); 
		}
	}
}